package org.jet.emall.rbac.transfer;

import org.jet.emall.rbac.dto.AdminMenuDTO;
import org.jet.emall.rbac.vo.AdminMenuVO;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;

import java.util.LinkedList;
import java.util.List;

/**
 * @author xiaozai
 * @version 1.0
 * @date 2020-04-05 11:16
 */
@Mapper
public interface AdminMenuVoTransfer {
    AdminMenuVoTransfer INSTANCE = Mappers.getMapper(AdminMenuVoTransfer.class);

    /**
     * 将DTO转VO
     * @param dto
     * @return
     */
    @Mapping(target = "children",ignore = true)
    AdminMenuVO toVo(AdminMenuDTO dto);


    /**
     * 将DTO集合转VO集合
     * @param dtoList
     * @return
     */
    default List<AdminMenuVO> toVoList(List<AdminMenuDTO> dtoList){
        //本方法有递归转换
        List<AdminMenuVO> result = new LinkedList<>();
        dtoList.forEach(value->{
            AdminMenuVO vo = toVo(value);
            if(value.getChildren()!=null&&!value.getChildren().isEmpty()){
                List<AdminMenuVO> children = toVoList(value.getChildren());
                vo.setChildren(children);
            }
            result.add(vo);
        });
        return result;
    }

}
